package com.walterweimod.auxiliary_attributes.event;

import com.walterweimod.auxiliary_attributes.capability.ModCapabilities;
import com.walterweimod.auxiliary_attributes.capability.impl.base.ChargeLivingEntityAttributeRangedValueBase;
import com.walterweimod.auxiliary_attributes.mobeffect.ModMobEffects;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.entity.LivingEntity;
import net.minecraftforge.event.entity.living.LivingHurtEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

import java.util.Objects;

import static com.walterweimod.auxiliary_attributes.Auxiliary_attributes.getLogger;
import static com.walterweimod.auxiliary_attributes.event.RageMainEvent.modifyAttributes;
import static com.walterweimod.auxiliary_attributes.util.ModCapabilityUtils.*;

@Mod.EventBusSubscriber
public class RageEvent {
    @SubscribeEvent
    public static void afterHurtUpdateRageAttribute(LivingHurtEvent event){
        LivingEntity livingEntity = event.getEntityLiving();
        MobEffectInstance rage = livingEntity.getEffect(ModMobEffects.RAGE.get());

        if (Objects.nonNull(rage)){
            setCapabilityValue(
                    livingEntity,
                    ModCapabilities.RAGE_LAYER_VALUE,
                    rageLayerValue -> rageLayerValue.addValue(1)
            );

            getLogger().info("受到攻击，目前战意层数为：{}", getCapabilityValue(
                    livingEntity,
                    ModCapabilities.RAGE_LAYER_VALUE,
                    ChargeLivingEntityAttributeRangedValueBase::getValue,
                    0
            ));

            modifyAttributes(livingEntity, rage.getAmplifier());
        }
    }
}
